﻿using System;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using _1906Ultras.Model;
using _1906Ultras.ViewModel;

namespace _1906Ultras
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        private MainViewModel _viewModel;
        public MainPage()
        {
            InitializeComponent();
            _viewModel = this.DataContext as MainViewModel;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            //App.Settings.AddOrUpdateValue("MainViewModel", _viewModel);
            App.Settings.EventList = _viewModel.Events;
            App.Settings.FeedList = _viewModel.Tweets;
            App.Settings.SongList = _viewModel.SongList;
            App.Settings.PhotoList = _viewModel.Photos;
//            IsolatedStorageSettings.ApplicationSettings["MainViewModel"] = _viewModel;
            base.OnNavigatedFrom(e);

        }



        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _viewModel = DataContext as MainViewModel;

            _viewModel.SongList = App.Settings.SongList;
            _viewModel.Events = App.Settings.EventList;
            _viewModel.Tweets = App.Settings.FeedList;
            _viewModel.Photos = App.Settings.PhotoList;

            base.OnNavigatedTo(e);

        }

        private void ImageClick(object sender, RoutedEventArgs e)
        {
            var selectedImage = ((Button)sender).DataContext as Photo;
            NavigationService.Navigate(new Uri("/PhotoViewer.xaml?&url=" + selectedImage.PhotoLink, UriKind.Relative));

        }


    }
}
